<template>
  <div class="wrapper">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'/5-1'}">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'/5-1'}">账号管理</el-breadcrumb-item>
      <el-breadcrumb-item >编辑账号</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form label-width="100px" :model="ruleForm" :rules="rules" ref="ruleForm">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="登录名" prop="username">
        <el-input v-model="ruleForm.username" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="账号角色" prop="actor">
        <el-select v-model="ruleForm.actor" placeholder="请选择角色">
          <el-option v-for="item in options" :label="item.label" :value="item.value" :key="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" v-show="ruleForm.showcode" type="password"><i slot="prefix" class="iconfont" @click="handleIconClick">&#xe618;</i></el-input>
        <el-input v-model="ruleForm.password" v-show="!ruleForm.showcode"><i slot="prefix" class="iconfont" @click="handleIconClick">&#xe63b;</i></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="goback">返回</el-button>
        <el-button type="warning" @click="submitForm('ruleForm')">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import axios from 'axios'
  import {EditAccount,updateAccount} from '@/api/accountmanagement/account'
  import {rolelist} from '@/api/role/role'
    export default {
        name: "EditAccount",
      data() {
        return {
          ruleForm: {
            showcode:'',
          },
          options: [],
          rules: {
            name: [
              {required: true, message: '请输入姓名', trigger: 'blur'},
              {min: 1, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'},
              {pattern: /^[\u4e00-\u9fa5]{0,}$/, message: '请输入中文姓名', trigger: 'blur'},
            ],
            username: [
              {required: true, message: '请输入登录名', trigger: 'blur'},
              {min: 3, max: 100, message: '登录名过短比较危险哦', trigger: 'blur'},
            ],
            actor: [
              { required: true, message: '请至少选择一个角色', trigger: 'change'}
            ],
            password: [
              {required: true, message: '请输入密码', trigger: 'blur'},
            ]
          }
        }
      },
      methods: {
        handleIconClick(){
          this.ruleForm.showcode=!this.ruleForm.showcode;
          console.log('11');
        },
        getDetailInfo() {
          localStorage.accounteEditIndex=this.$route.params.index?this.$route.params.index:localStorage.accounteEditIndex;
          let id=this.$route.params.index?this.$route.params.index:localStorage.accounteEditIndex;
          console.log(id);
          EditAccount(id).then(this.handleGetDataSucc);
        },
        getoptions(){
          let params={};
          params.page=1;
          params.size=10;
          params.updateBy=JSON.parse(localStorage.userinfo).id;
          let _this=this;
          rolelist(params).then(function (res) {
            console.log(res);
            let rolename=res.data.UserList;
           for(let item of rolename){
             _this.options.push({
               value:item.actor,
               label:item.actor
             })
           }
          })
        },
        handleGetDataSucc(res) {
          res.data.showcode=true;
          this.ruleForm=res.data;
        },
        submitForm(formName) {
          let id=this.$route.params.index;
          this.$refs[formName].validate((valid) => {
            if (valid) {
              this.$confirm('确定要修改后台账号吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'success',
                center: true
              }).then(() => {
                this.$message({
                  type: 'success',
                  message: '已更新后台账号!'
                });
                console.log(this.ruleForm);
                var _this=this;
                updateAccount(id,this.ruleForm).then(function (res) {
                  console.log(res);
                  _this.$router.push({name:'AccountManagement'})
                })
              }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消更新'
                });
              })
            } else {
              return false;
            }
          });
        },
        goback(){
          this.$router.push({name:'AccountManagement'})
        }
      },
      mounted (){
          this.getDetailInfo();
        this.getoptions();
      }
    }
</script>

<style lang="less" scoped>
  @import "~styles/z.less";
  @import "~styles/css3.less";
  @import "~styles/variables.less";

</style>
